/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import pojo.Chitietchuongtrinhkm;
import util.HibernateUtil;

/**
 *
 * @author sangnguyen
 */
public class ChiTietChuongTrinhKhuyenMaiDAO {
    public static boolean InsertChitietchuongtrinhkm(Chitietchuongtrinhkm obj){
        Session session = HibernateUtil.getSessionFactory().openSession();        
        Transaction tx = session.beginTransaction();
        try{
            
            session.save(obj);
            tx.commit();
        }
        catch(HibernateException ex){
            tx.rollback();
            System.err.println(ex);
            return false;
        }
        finally{
            session.close();
        }

        return true;
    }
    public static boolean UpdateChitietchuongtrinhkm(Chitietchuongtrinhkm obj){
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();    
        try{
            session.update(obj);
            tx.commit();
        }
        catch(HibernateException ex){
            tx.rollback();
            System.err.println(ex);
            return false;
        }
        finally{
            session.close();                            
        }
        return true;
    }
     public static boolean DeleteChitietchuongtrinhkm(Chitietchuongtrinhkm obj){
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        try{
            obj.setTinhtrang(0);
            session.save(obj);
            tx.commit();
        }
        catch(HibernateException ex){
            tx.rollback();
            System.err.println(ex);
            return false;
        }
        finally{
            session.close();        
        }
        return true;
    }
     public static List<Chitietchuongtrinhkm> SelectAllChitietchuongtrinhkm(int idCTKM){
         List<Chitietchuongtrinhkm> lsChitietchuongtrinhkm = null;
         Session session = HibernateUtil.getSessionFactory().openSession();   
         try{
            Query query = session.createQuery("Select ctctkm from Chitietchuongtrinhkm as ctctkm inner join ctctkm.chitietsanpham as ctsp inner join ctsp.sanpham as sp where ctctkm.tinhtrang=:tinhTrang and ctctkm.chuongtrinhkhuyenmai.idctkm=:idCTKM order by ctctkm.id asc");
            query.setInteger("tinhTrang", 1);
            query.setInteger("idCTKM", idCTKM);
            lsChitietchuongtrinhkm = query.list();         
         }
         catch(HibernateException ex){            
            System.err.println(ex);
         }
         finally{
            session.close();        
         }
         return lsChitietchuongtrinhkm;
     }
     public static Chitietchuongtrinhkm GetChitietchuongtrinhkmID(int id){
         Chitietchuongtrinhkm obj = new Chitietchuongtrinhkm();
         Session session = HibernateUtil.getSessionFactory().openSession();    
         try{
         obj = (Chitietchuongtrinhkm)session.get(Chitietchuongtrinhkm.class, id);         
         }
         catch(HibernateException ex){
             System.err.println(ex);
         }
         finally{
            session.close();        
         }
         return obj;
     }
     public static Chitietchuongtrinhkm GetChitietchuongtrinhkmSP(int idCTKM, int idCTSP){
         Chitietchuongtrinhkm obj = new Chitietchuongtrinhkm();
         Session session = HibernateUtil.getSessionFactory().openSession();    
         try{
            Query query = session.createQuery("select ctctkm from Chitietchuongtrinhkm as ctctkm where ctctkm.tinhtrang=:tinhTrang and ctctkm.chuongtrinhkhuyenmai.idctkm=:idCTKM and ctctkm.chitietsanpham.idctsp=:idCTSP");
            query.setInteger("tinhTrang", 1);
            query.setInteger("idCTKM", idCTKM);
            query.setInteger("idCTSP", idCTSP);
            obj = (Chitietchuongtrinhkm)query.uniqueResult();
            return obj;
         }
         catch(HibernateException ex){
             System.err.println(ex);
             return null;
         }
         finally{
            session.close();        
         }
         //return obj;
     }
}
